<?php
/*
---------------------------------------------------------------------------
admin/hosts.php for SALAM Frontend

Copyright 2009, 2010 Jacob McEntire

    This file is part of SALAM.

    SALAM is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    SALAM is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with SALAM.  If not, see <http://www.gnu.org/licenses/>.
---------------------------------------------------------------------------
*/
require("../../library/frontend.php");
require("functions.php");

if (isset($_GET['action']))
	$action = check_xss($_GET['action']);
else
	$action = 'show_hosts';
	
switch ($action) {
	case 'add':
		page_start("Add Host", TRUE);
		if (isset($_POST['name'])) {
			$name = check_xss($_POST['name']);
			$dns_name_or_ip = check_xss($_POST['dns_name_or_ip']);
			$location = check_xss($_POST['location']);
			$group = check_xss($_POST['group']);
			$trigger = $_POST['trigger'];
			if ($_POST['enable'] == 1)
				$enabled = 1;
			else
				$enabled = 0;
			$host_id = run_query_return_id("INSERT INTO hosts (name, dns_name_or_ip, location_id, group_id, enable_monitoring) VALUES ('$name', '$dns_name_or_ip', '$location', '$group', '$enabled')");
			foreach ($trigger as $trigger_id) {
				run_query("INSERT INTO hosts_triggers (host_id, trigger_id) VALUES ('$host_id', '$trigger_id')");
			}
			
				echo '<h3>', $name, ' Added Successfully</h3>
				<a href="hosts.php">Back to Hosts</a> | <a href="hosts.php?action=add">Add Another Host</a>';					
		}
		else
			add_host_form('','','','','');
		page_end();
		break;
	case 'edit':
		page_start("Edit Host", TRUE);
		if (isset($_POST['id'])) {
			$id = check_xss($_POST['id']);
			$name = check_xss($_POST['name']);
			$dns_name_or_ip = check_xss($_POST['dns_name_or_ip']);
			$location = check_xss($_POST['location']);
			$group = check_xss($_POST['group']);
			$trigger = $_POST['trigger'];
			if ($_POST['enable'] == 1)
				$enabled = 1;
			else
				$enabled = 0;
			run_query("UPDATE hosts SET name = '$name', dns_name_or_ip = '$dns_name_or_ip', location_id = '$location', group_id = '$group', enable_monitoring = '$enabled' WHERE id = '$id' LIMIT 1");
			$ctresult  = run_query("SELECT trigger_id FROM hosts_triggers WHERE host_id = '$id'");
			if(mysql_num_rows($ctresult) != 0) {
				while($row = mysql_fetch_array($ctresult))
					$current_triggers[] = $row[0];
			}
			else
				$current_triggers[] = NULL;
			if ($trigger == "")
				$trigger[] = NULL;
			$result = run_query("SELECT id FROM triggers");
			if(mysql_num_rows($result) != 0) {
				while(list($avail_id) = mysql_fetch_array($result)) {
					if (in_array($avail_id, $trigger) == TRUE AND in_array($avail_id, $current_triggers) == FALSE) {
						run_query("INSERT INTO hosts_triggers (host_id, trigger_id) VALUES ('$id', '$avail_id')");
					}
					elseif (in_array($avail_id, $trigger) == FALSE AND in_array($avail_id, $current_triggers) == TRUE) {
						run_query("DELETE FROM hosts_triggers WHERE host_id = '$id' AND trigger_id = '$avail_id' LIMIT 1");
						run_query("DELETE FROM trigger_data WHERE host_id = '$id' AND trigger_id = '$avail_id' LIMIT 1");
						run_query("DELETE FROM alerts WHERE host_id = '$id' AND trigger_id = '$avail_id' LIMIT 1");
					}
				}
			}
			echo '<h3>', $name, ' Saved Successfully</h3>
			<a href="hosts.php">Back to Hosts</a>';
		}
		elseif (isset($_GET['id']))
			edit_host_form(check_xss($_GET['id']));
		else
			echo 'ERROR';
		page_end();
		break;
	case 'confirm':
		page_start("Delete Host", TRUE);
		if (isset($_GET['name']) && isset($_GET['id'])) {
			$id = check_xss($_GET['id']);
			$name = check_xss($_GET['name']);
			echo '<h3>Are you sure you want to delete ', $name, ' and all of it\'s data?</h3>
			<a href="hosts.php?action=delete&name=', $name, '&id=', $id, '">Confirm</a> | <a href="hosts.php">Cancel</a>';
		}
		else
			echo 'ERROR';
		page_end();
		break;
	case 'delete':
		page_start("Delete Host", TRUE);
		if (isset($_GET['name']) && isset($_GET['id'])) {
			$id = check_xss($_GET['id']);
			$name = check_xss($_GET['name']);
			$result = run_query("DELETE FROM hosts WHERE id = '$id' LIMIT 1");
				if ($result)
					$result = run_query("DELETE FROM hosts_triggers WHERE host_id = '$id'");
				if ($result)
					$result = run_query("DELETE FROM trigger_data WHERE host_id = '$id'");
				if ($result)
					$result = run_query("DELETE FROM alerts WHERE host_id = '$id'");
				if ($result)
					echo '<h3>', $name, ' Deleted Successfully</h3><a href="hosts.php">Back to Hosts</a>';
				else
					echo '<h3>Error Deleting ', $name, '</h3><a href="hosts.php">Back to Hosts</a>';
		}
		else
			echo 'ERROR';
		page_end();
		break;
	default:
		page_start("Hosts", TRUE);
		show_hosts();
		echo '<div class="pagemenu"><h3>Options:</h3><a href="hosts.php?action=add">Add a Host</a></div>';
		page_end();
		break;
}
?>